#include "Projectile.h"

Projectile::Projectile(Point& location)
	:Movables(location, RADIUS_OF_MOVABLE, 1, PROJECTILE, getDefaultBitmap(PROJECTILE)),
	_is_shooting(false)
{}

DetectionResult Projectile::collisionCheck(Movable_type position_type) const
{
	switch(position_type)
	{
	case HOLE:
		return CAN_MOVE;
		break;
	case SAND:
		return COLLISION;
		break;
	case MONSTER:
		return MONSTER_DEAD;
		break;
	case DIGDUG:			//Should never happen!
		return DIGDUG_DEAD;
		break;
	}
	return DIGDUG_DEAD;		//Should never reach here!
}

DetectionResult Projectile::move(Map &map)
{
	

	DetectionResult move_result = checkMoveInDirection(_location, _direction, map);
	
	if ( move_result == CAN_MOVE)
	{
		int delta_x = delta(_direction).at(0);
		int delta_y = delta(_direction).at(1);
		_location = Point(_location.x() + delta_x, _location.y() + delta_y);
	}
	
	return move_result;
}

DetectionResult Projectile::initializeShot(Direction direction, Point& digdug_location, int digdug_radius,  Map& map)
{
	_direction = direction;
		
	/*! \brief Set Projectile to one position before where it supposed to be. 
			
		If it can move into its position then shooting is allowed, if not then digdug cannot shoot.
		If shooting is allowed then the projectile is updated to its correct positiion in game when it is moved.
	*/
	
	int delta_x = delta(_direction).at(0);
	int delta_y = delta(_direction).at(1);

	int new_x = digdug_location.x() + delta_x*(digdug_radius + _radius);
	int new_y = digdug_location.y() + delta_y*(digdug_radius + _radius);
			
	_location = Point(new_x, new_y);
	
	_is_shooting = true;

	DetectionResult move_result = move(map);

	if(move_result != CAN_MOVE)
	{
		_is_shooting = false;
	}
	return move_result;
}

void Projectile::shoot(bool set)
{
	_is_shooting = set;
}

bool Projectile::isShooting()
{
	return _is_shooting;
}